package com.wyp.java8;


import java.util.*;
import java.util.stream.Collectors;

public class StreamDemo4 {
    public static void main(String[] args) {

        User u1=new User(11,"f",23, User.Status.FREE);
        User u2=new User(12,"m",24, User.Status.BUSY);
        User u3=new User(13,"c",22, User.Status.VOCATION);
        User u4=new User(14,"d",28, User.Status.FREE);
        User u5=new User(16,"e",27, User.Status.VOCATION);
        User u6=new User(16,"e",27, User.Status.VOCATION);

        List<User> list = Arrays.asList(u1,u2,u3,u4,u5,u6);

//        //获取用户名 放入list
//        list.stream().map(e -> {return e.getName();}).collect(Collectors.toList()).forEach(System.out::println);
//
//         //放入set
//        list.stream().map(e->{return e.getName();}).collect(Collectors.toSet()).forEach(System.out::println);
//
//         //放入其他类型
//        list.stream().map(e ->{return e.getName();}).collect(Collectors.toCollection(HashSet::new)).forEach(System.out::println);
        //总数
        System.out.println(list.stream().collect(Collectors.counting()));

        System.out.println(list.stream().map(e -> {
            return e.getName();
        }).collect(Collectors.counting()));

        //平均值
        System.out.println(list.stream().collect(Collectors.averagingInt(User::getAge)));

        //最大值
        System.out.println(list.stream().collect(Collectors.maxBy((e1, e2) -> {
            return e1.getAge().compareTo(e2.getAge());
        })));
        // 最小值 年纪
        System.out.println(list.stream().map(e -> {
            return e.getAge();
        }).collect(Collectors.minBy((e1, e2) -> {
            return e1.compareTo(e2);
        })));

        // 分组
        Map<User.Status, List<User>> collect = list.stream().collect(Collectors.groupingBy(e -> {
            return e.getStatus();
        }));
        System.out.println(collect);

        // 多级分组
        System.out.println(list.stream().collect(Collectors.groupingBy(e -> {
            return e.getStatus();
        }, Collectors.groupingBy(e -> {
            if (e.getAge() <= 24){
                return "青年";
            }else{
                return "中年";
            }
        }))));

        // 分区  分 true 和 false
        System.out.println(list.stream().collect(Collectors.partitioningBy(e -> {
            return e.getAge() > 24;
        })));

        // 字段总结
        System.out.println(list.stream().collect(Collectors.summarizingInt(User::getAge)));
        System.out.println(list.stream().collect(Collectors.summarizingInt(e->{return e.getAge();})));
        // IntSummaryStatistics{count=6, sum=151, min=22, average=25.166667, max=28}

        //  连接
        System.out.println(list.stream().map(e -> {
            return e.getName();
        //}).collect(Collectors.joining()));
        }).collect(Collectors.joining("-")));
    }
}


